package com.sinoglobal.ningxia.activity.amusement;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.PopupClickListener;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.AmusementInfoVo;
import com.sinoglobal.ningxia.beans.AmusementListVo;
import com.sinoglobal.ningxia.fragment.SettleDownInfoFragment;
import com.sinoglobal.ningxia.utils.StringUtil;

/* 
 * 要处理overlay点击事件时需要继承ItemizedOverlay 
 * 不处理点击事件时可直接生成ItemizedOverlay. 
 */
public class MyOverlay extends ItemizedOverlay<OverlayItem> {
	// 用MapView构造ItemizedOverlay
	private PopupOverlay pop;
	private View inflate;
	private TextView text1; // 安居 娱乐 美食 名称
	private MapView mMapView;
	private TextView text10; // 位置信息
	private TextView text11; // 电话号码
	private AmusementListVo amusementListVo; // 信息集合
	private Context context;
	private AmusementInfoVo amusementInfoVo; // 单条信息

	public MyOverlay(Context context, Drawable mark, MapView mapView,
			AmusementListVo amusementListVo) {
		super(mark, mapView);
		this.context = context;
		this.amusementListVo = amusementListVo;
		this.mMapView = mapView;
		inflate = LayoutInflater.from(context).inflate(
				R.layout.shifu_location_view, null);
		text1 = (TextView) inflate.findViewById(R.id.text1);
		text10 = (TextView) inflate.findViewById(R.id.text10);
		text11 = (TextView) inflate.findViewById(R.id.text11);
	}

	protected boolean onTap(final int index) {
		amusementInfoVo = amusementListVo.getList().get(index);
		text1.setText(amusementInfoVo.getShop_name());
		text10.setText(amusementInfoVo.getShop_address());
		if (StringUtil.isNullOrEmpty(amusementInfoVo.getShop_tel())) {
			text11.setText(context.getString(R.string.nofound));
		} else {
			text11.setText(amusementInfoVo.getShop_tel());
		}
		if ("3".equals(FilterRequirement.TYPE)) {
			text1.setText(amusementInfoVo.getHouse_name());
			text10.setText(amusementInfoVo.getHouse_address());
			if (StringUtil.isNullOrEmpty(amusementInfoVo.getHouse_tel())) {
				text11.setText(context.getString(R.string.nofound));
			} else {
				text11.setText(amusementInfoVo.getHouse_tel());
			}
		}
		// 在此处理item点击事件
		pop = new PopupOverlay(mMapView, new PopupClickListener() {
			@Override
			public void onClickedPopup(int arg0) {
				Intent intent = new Intent();
				if (FilterRequirement.ENTERTAINMENT
						.equals(FilterRequirement.TYPE)
						|| FilterRequirement.CATE
								.equals(FilterRequirement.TYPE)) {
					intent.setClass(context, BusinessDetailActivity.class);
					intent.putExtra(BusinessDetailActivity.SHOPID,
							amusementInfoVo.getId());
					intent.putExtra(BusinessDetailActivity.SHOPNAME,
							amusementInfoVo.getShop_name());
					intent.putExtra(BusinessDetailActivity.TYPEID,
							FilterRequirement.TYPE);
				} else {
					intent.setClass(context, SettleDownDetailsActivity.class);
					intent.putExtra(SettleDownInfoFragment.HOUSEID,
							amusementInfoVo.getId());
				}
				context.startActivity(intent);
			}
		});
		/**
		 * 准备pop弹窗资源，根据实际情况更改 弹出包含三张图片的窗口，可以传入三张图片、两张图片、一张图片。
		 * 弹出的窗口，会根据图片的传入顺序，组合成一张图片显示. 点击到不同的图片上时，回调函数会返回当前点击到的图片索引index
		 */
		// Bitmap[] bmps = new Bitmap[1];
		// bmps[0] = BitmapFactory.decodeResource(context.getResources(),
		// R.drawable.img_xingji_xiala_pressed);
		// 弹窗弹出位置
		GeoPoint ptTAM = new GeoPoint(
				(int) (Double.parseDouble(amusementInfoVo.getLatitude()) * 1E6),
				(int) (Double.parseDouble(amusementInfoVo.getLongitude()) * 1E6));
		// 弹出pop,隐藏pop
		// pop.showPopup(bmps, ptTAM, 32);
		pop.showPopup(inflate, ptTAM, 120);
		// 隐藏弹窗
		// pop.hidePop();
		return true;
	}

	public boolean onTap(GeoPoint pt, MapView mapView) {
		if (pop != null) {
			pop.hidePop();
		}
		// 在此处理MapView的点击事件，当返回 true时
		super.onTap(pt, mapView);
		return false;
	}
	// 自2.1.1 开始，使用 add/remove 管理overlay , 无需重写以下接口
	/*
	 * @Override protected OverlayItem createItem(int i) { return
	 * mGeoList.get(i); }
	 * 
	 * @Override public int size() { return mGeoList.size(); }
	 */
}